from django.shortcuts import render_to_response
from django.http import HttpResponse
from Data_com.models import *
from django.http import HttpResponseRedirect

def ex(request):
    return render_to_response('main.html')

def search_page(request):
    return render_to_response('search_page.html')

def search_show(request):
    return render_to_response('search_show.html')

def edit (request):
    return render_to_response('edit.html')

def edit_run (request):
    return render_to_response('edit_data.html')

def edit_run_run (request):

    error = []
	
    if 'number' in request.GET and 'status' in request.GET and 'symptoms' in request.GET and 'fix_time' in request.GET:
	
		number = request.GET['number']
		status = request.GET['status']
		symptoms = request.GET['symptoms']
		fix_time = request.GET['fix_time']
		
		if not number:
			error.append('Enter your ID.')
		if not status:
			error.append('Enter your status.')
		if not symptoms:
			error.append('Enter your symptoms.')
		if not fix_time:
			error.append('Enter your fix_time.')
		if error:
			return render_to_response('edit_data.html',{'errors': error})

		students1 = List_com.objects.filter(number=number).delete()
		
		Save_data = List_com.objects.create(number=number,status=status,symptoms=symptoms,fix_time=fix_time)
		Save_data.save()

    students = List_com.objects.filter(number=number)
    return render_to_response('search_show.html',{'students': students, 'query': number})    
def create (request):
    return render_to_response('create.html')

def delete (request):
    return render_to_response('delete.html')

def delete_run(request):
    if 'q' in request.GET and request.GET['q']:
		q = request.GET['q']
		students1 = List_com.objects.filter(number=q).delete()
		students = List_com.objects.all()
                q = u'All'
                return render_to_response('search_show.html',{'students': students, 'query': q})
    else:
		return render_to_response('Delete.html', {'error': True})

def search(request):
    if 'q' in request.GET and request.GET['q']:
		q = request.GET['q']
		students = List_com.objects.filter(number=q)
		return render_to_response('search_show.html',
			{'students': students, 'query': q})
    else:
		return render_to_response('search_page.html', {'error': True})

def show_all(request):
    students = List_com.objects.all()
    q = u'All'
    return render_to_response('search_show.html',{'students': students, 'query': q})

def create_run(request):

    error = []
	
    if 'number' in request.GET and 'status' in request.GET and 'symptoms' in request.GET and 'fix_time' in request.GET:
	
		number = request.GET['number']
		status = request.GET['status']
		symptoms = request.GET['symptoms']
		fix_time = request.GET['fix_time']
		
		if not number:
			error.append('Enter your ID.')
		if not status:
			error.append('Enter your status.')
		if not symptoms:
			error.append('Enter your symptoms.')
		if not fix_time:
			error.append('Enter your fix_time.')
		if error:
			return render_to_response('create.html',{'errors': error})
		
		Save_data = List_com.objects.create(number=number,status=status,symptoms=symptoms,fix_time=fix_time)
		Save_data.save()

    students = List_com.objects.filter(number=number)
    return render_to_response('search_show.html',{'students': students, 'query': number})
